package com.nfha.algorithm.test;

import static org.junit.Assert.assertNotNull;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Deque;

import org.junit.Before;
import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import com.nfha.algorithm.geo.navigation.AstarNavigationExerciser;
import com.nfha.algorithm.geo.navigation.GeoMap;
import com.nfha.algorithm.geo.navigation.GeoMapNode;
import com.nfha.algorithm.geo.navigation.GeoPoint;
import com.nfha.algorithm.util.IoUtil;

public class NavigationExerciserTester {

	Gson gson = new Gson();

	File jsonFile = new File(IoUtil.getPwdResource().getPath() + "input/navigation/aaa.json"),
			mapFile = new File(IoUtil.getPwdResource().getPath() + "input/navigation/清明上河图.map")
			;
	double[][] polygonGeoPoints;

	GeoMap geoMap ;
	
	//测试起点和终点
	GeoMapNode gmn0,gmn1;
	
	AstarNavigationExerciser astarNavigationExerciser = new AstarNavigationExerciser();
	
	@Before
	public void init() throws FileNotFoundException, IOException, ClassNotFoundException {
		// fail("Not yet implemented");
		JsonArray coordinates = readCoordinatesFromJson(jsonFile);

		 polygonGeoPoints = new double[coordinates.size()][2];
		for (int i = 0; i < coordinates.size(); i++) {
			JsonArray coordinatei = coordinates.get(i).getAsJsonArray();
			double lon = coordinatei.get(0).getAsDouble(), lat = coordinatei.get(1).getAsDouble();
			polygonGeoPoints[i] = new double[] { lon, lat };
		}
		//构建多边形地图
		if(mapFile.exists()) {
			//如果之前有持久化，可以直接读文件出来，减少构建地图的性能损耗
			geoMap=astarNavigationExerciser.readObject(mapFile);
		}else {
			//如果没有持久化，就自己new出来，然后保存作为下次使用
		    geoMap = new GeoMap(400, 400, 1, 0, 0, polygonGeoPoints);
		    mapFile.createNewFile();
			astarNavigationExerciser.saveObject(geoMap, mapFile);
		} 
		gmn0 = geoMap.getMapNodeByIndex(20, 11);
		 gmn1 = geoMap.getMapNodeByIndex(380, 397);
	}

	
	

	protected JsonArray readCoordinatesFromJson(File json) throws FileNotFoundException, IOException {
		JsonArray ret = null;
		try (FileReader fr = new FileReader(jsonFile);
				BufferedReader br = new BufferedReader(fr);
				JsonReader jr = new JsonReader(br)) {

			JsonObject FeatureCollection = JsonParser.parseReader(jr).getAsJsonObject();
			ret = FeatureCollection.getAsJsonArray("features").get(0).getAsJsonObject().getAsJsonObject("geometry")
					.getAsJsonArray("geometries").get(0).getAsJsonObject().getAsJsonArray("coordinates").get(0)
					.getAsJsonArray()

			;

		}
		return ret;
	}
	
	
	/*
	 * 直接使用多形性数组计算，虽然使用方便，但每次要重建地图会造成性能损耗
	 */
	@Test
	public void testExec0() throws Exception {
		
		GeoPoint gp0 = new GeoPoint("", gmn0.lon, gmn0.lat), gp1 = new GeoPoint("", gmn1.lon, gmn1.lat);
		Deque<GeoMapNode> results = astarNavigationExerciser.exec(gp0, gp1, polygonGeoPoints);
		assertNotNull(results);
		geoMap.print(results);
	}

	/*
	 * 使用持久化地图方式，能减少构建地图的损耗，但需要自行管理序列化文件
	 */
	@Test
	public void testExec1() throws Exception {
		//构建多边形地图
		GeoPoint gp0 = new GeoPoint("", gmn0.lon, gmn0.lat), gp1 = new GeoPoint("", gmn1.lon, gmn1.lat);
		Deque<GeoMapNode> results = astarNavigationExerciser.exec(gp0, gp1, geoMap);
		assertNotNull(results);
		//geoMap.print(results);
	}

}
